package stardust.product.utils.monitor;


import java.io.IOException;


public class ProcessMonitor
{

  public static void main(String[] args) throws IOException,
                                                InterruptedException
  {
    new MonitorThread("G:\\OS\\系统\\Firefox Setup 3.0.6.exe").start();
    
    for (String p : args)
    {
      //new MonitorThread("D:\\OS\\系统\\Firefox Setup 3.0.6.exe").start();
      System.out.println("启动[" + p + "]...");
      new MonitorThread(p).start();
    }
  }
  public static class MonitorThread extends Thread
  {

    private String program;

    public MonitorThread(String program)
    {
      this.program = program;
    }

    public void run()
    {
      while (true)
      {
        ProcessBuilder pb = new ProcessBuilder(this.program);
        Process p = null;
        try
        {
          p = pb.start();
          p.waitFor();
          p.destroy();
        }
        catch (Throwable e)
        {
          System.out.println("监控程序[" + this.program + "]遇到问题，正在重新启动。");
          if (p != null)
          {
            p.destroy();
          }
        }
        finally
        {
          try
          {
            Thread.sleep(1000);
          }
          catch (InterruptedException e)
          {
            ;
          }
        }
      }
    }
  }
}
